//
//  DLStore.m
//  IpPrivacy
//
//  Created by 录音转文字助手 on 2020/8/21.
//  Copyright © 2020 录音转文字助手. All rights reserved.
//

#import "DLStore.h"
#import "XYStoreiTunesReceiptVerifier.h"
#import "XYStoreKeychainPersistence.h"
#import "XYStoreUserDefaultsPersistence.h"


static DLStore *class = nil;
@implementation DLStore
#pragma mark -- 单利方法重写 --
+ (id)allocWithZone:(struct _NSZone *)zone{
    if (!class) {
        class = [super allocWithZone:zone];//如果没有实例让父类去创建一个
        return class;
    }
    return nil;
}
- (id)copyWithZone:(struct _NSZone *)zone{
    return self;
}
+ (instancetype) singletonMod {
    @synchronized(self) {
        if (class == nil) {
            class = [[self alloc] init];
        }
    }
    return class;
}
-(void)initData{
    [[XYStoreiTunesReceiptVerifier shareInstance]setSharedSecretKey:payId];
    // 在此设置内购的票据校验，防止掉单问题的发生
    [[XYStore defaultStore] registerTransactionPersistor:[XYStoreKeychainPersistence shareInstance]];
    [[XYStore defaultStore] registerTransactionPersistor:[XYStoreUserDefaultsPersistence shareInstance]];
    [[XYStore defaultStore] registerReceiptVerifier:[XYStoreiTunesReceiptVerifier shareInstance]];
    NSSet *set = [[NSSet alloc] initWithArray:comArr];
    [[XYStore defaultStore] requestProducts:set];
}


-(void)getComListSuccess:(void (^)(NSArray *products, NSArray *invalidProductIdentifiers))successBlock
                 failure:(void (^)(NSError *error))failureBlock{

    NSSet *set = [[NSSet alloc] initWithArray:comArr];
    [[XYStore defaultStore] requestProducts:set success:^(NSArray *products, NSArray *invalidProductIdentifiers){
        if (successBlock){
            successBlock(products,invalidProductIdentifiers);
        }
        NSLog(@"%@",products);
    } failure:^(NSError *error) {
        if (failureBlock){
            failureBlock(error);
        }
        NSLog(@"%@",error);
    }];
}

/**
 @ 适用自动续期订阅
 
 判断是否已订阅
 若果是非自动续费型商品，直接返回NO
 
 @return YES：  NO：未订阅或者订阅过期
 */
-(BOOL)dl_isSubscribedWithAutoRenewProduct{    
#if DEBUG
    return true;
#endif
    NSArray *array = comArr;
    for (NSString *productId in array) {
        BOOL bol = [[XYStoreiTunesReceiptVerifier shareInstance] isSubscribedWithAutoRenewProduct:productId];
        if (bol){
            return true;
        }
    }
    return false;
}

/**
 恢复购买
 */
-(void)dl_restoreTransactionsOnSuccess:(void (^)(NSArray *transactions))successBlock
                               failure:(void (^)(NSError *error))failureBlock{
    [[XYStore defaultStore] restoreTransactionsOnSuccess:^(NSArray *transactions) {
        if (successBlock){
            successBlock(transactions);
        }
        NSLog(@"%@",transactions);
    } failure:^(NSError *error) {
        if (failureBlock){
            failureBlock(error);
        }
        NSLog(@"%@",error);
    }];
}

/**
 购买服务
 */
-(void)dl_addPayment:(NSString *)paymentId
             success:(void (^)(SKPaymentTransaction *transaction))successBlock
             failure:(void (^)(SKPaymentTransaction *transaction, NSError *error))failureBlock{
    

    //    @"1monthVIP"
    [[XYStore defaultStore] addPayment:paymentId success:^(SKPaymentTransaction *transaction) {
        if (successBlock){
            successBlock(transaction);
        }
        [[XYStore defaultStore] restoreTransactions];
        NSLog(@"%@",transaction);
    } failure:^(SKPaymentTransaction *transaction, NSError *error) {
        if (failureBlock){
            failureBlock(transaction,error);
        }
        NSLog(@"%@",error);
    }];
}

@end
